﻿using SharpDepend.WP8;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SharpDepend.WinRT.Controls
{
    public sealed partial class GraphicsDisplay : UserControl, IGraphicsDisplay
    {
        private IGraphicsDevice mCurrentDevice;

        public GraphicsDisplay()
        {
            this.InitializeComponent();

            Loaded += GraphicsDisplay_Loaded;
            Unloaded += GraphicsDisplay_Unloaded;
        }

        void GraphicsDisplay_Loaded(object sender, RoutedEventArgs e)
        {
            // If mCurrentDevice is not set. Then this control is created in XAML directly.
            if (mCurrentDevice == null)
            {
                (SharpDepend.Manager.GraphicsDisplayManager as GraphicsDisplayManager).SetupDisplay(this);
            }

            var call = DeviceLoaded;
            if (call != null)
            {
                call();
            }
        }

        void GraphicsDisplay_Unloaded(object sender, RoutedEventArgs e)
        {
            if (mCurrentDevice != null)
            {
                mCurrentDevice.Dispose();
                mCurrentDevice = null;
            }

            var call = DeviceUnloaded;
            if (call != null)
            {
                call();
            }
        }

        public event Action DeviceLoaded;

        public event Action DeviceUnloaded;

        public void GraphicsDeviceChanged(IGraphicsDevice device)
        {
            if (mCurrentDevice != null)
            {
                mCurrentDevice.Dispose();
                Content = null;
            }

            mCurrentDevice = device;
            Content = (UIElement)device;
            Content.InvalidateArrange();
        }

        public IGraphicsDevice GraphicsDevice
        {
            get { return mCurrentDevice; }
        }

        public event Classes.EventHandlerMouseDownAction PointDown;

        public event Classes.EventHandlerMouseAction PointMove;

        public event Classes.EventHandlerMouseAction PointUp;

        public event Classes.EventHandlerMouseWheelAction PointWheel;

        public bool OnMouseDown(ref Datatypes.TouchLocation location, int id, int touchCount)
        {
            var call = PointDown;
            if (call != null)
            {
                return call(ref location, id, touchCount);
            }
            return false;
        }

        public void OnMouseMove(ref Datatypes.TouchLocation location, int id, int touchCount)
        {
            var call = PointMove;
            if (call != null)
            {
                call(ref location, id, touchCount);
            }
        }

        public void OnMouseUp(ref Datatypes.TouchLocation location, int id, int touchCount)
        {
            var call = PointUp;
            if (call != null)
            {
                call(ref location, id, touchCount);
            }
        }

        public void OnMouseWheel(ref Datatypes.TouchLocation location)
        {
            var call = PointWheel;
            if (call != null)
            {
                call(ref location);
            }
        }
    }
}
